
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.geom.AffineTransform;

public class ViewPanelMenu extends AbstractView {

	private PanelMenu panelMenu;
	private ControllerMenu controller;
	
	/*
	 * Class PanelMenu implements the panel 
	 */
	public class PanelMenu extends JPanel{
		
		private static final long serialVersionUID = 1L;
		
		/*
		 * Inner class constructor
		 */
		public PanelMenu() {
			setLayout(null);
			addComponents();
		}

		public void paintComponent(Graphics g) {
	        Graphics2D g2D = (Graphics2D) g;

	        /*
	         * Draw background image
	         */
			double imageWidth = WorldWar.imageLoader.getImageByName(ImageStructure.IMAGE_NAME_PANEL_MENU_BACKGROUND).getWidth();
	        double imageHeight = WorldWar.imageLoader.getImageByName(ImageStructure.IMAGE_NAME_PANEL_MENU_BACKGROUND).getHeight();
	        double panelWidth = this.getWidth();
	        double panelHeight = this.getHeight();

	        double sx = (panelWidth / imageWidth);
	        double sy = (panelHeight / imageHeight);
	        
	        g.clearRect(0, 0, 800, 700);
	        AffineTransform af = AffineTransform.getScaleInstance(sx, sy);
	        g2D.drawImage(WorldWar.imageLoader.getImageByName(ImageStructure.IMAGE_NAME_PANEL_MENU_BACKGROUND), af, this);
		}
		
		/*
	     * Initialize the basic screen
	     */
		private void addComponents() {		
			/*
			 * Clear the panel
			 */
			this.removeAll();
			
	        /*
	         * Initialize the buttons
	         */
	        JButton buttonSinglePlayer = new JButton("Single Player");
	        JButton buttonMultiPlayers = new JButton("Multiple Players");
	        JButton buttonOptions = new JButton("Options");
	        JButton buttonExit = new JButton("Exit");
	        
	        buttonSinglePlayer.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH-750) / 2, 480, 189, 50);
	        buttonMultiPlayers.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH -400) / 2, 550, 189, 50);
	        buttonOptions.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH ) / 2, 550, 189, 50);
	        buttonExit.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH + 350) / 2, 480, 189, 50);
	        
	        /*
	         * Add ActionListener to buttons
	         */
	        buttonSinglePlayer.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	System.out.println();
	            	controller.buttonPressedSinglePlayer();
	            }
	        });
	        
	        buttonMultiPlayers.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	System.out.println();
	            	controller.buttonPressedMultiPlayers();
	            }
	        });
	        
	        buttonOptions.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	System.out.println();
	            	controller.buttonPressedOptions();
	            }
	        });
	        
	        buttonExit.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	System.out.println();
	            	controller.buttonPressedExit();
	            }
	        });
	        
	        /*
	         * Add buttons to the menu
	         */
	        this.add(buttonSinglePlayer);
	        this.add(buttonMultiPlayers);
	        this.add(buttonOptions);
	        this.add(buttonExit);
	        
	        /*
			 * Refresh
			 */
	        this.repaint();
		}
	}
	
	/*
	 * Constructor
	 */
	public ViewPanelMenu() {
		panelMenu = new PanelMenu();
	}

	/*
	 * Accessors
	 */
	public JPanel getPanel() {
		return panelMenu;
	}
	
	public ControllerMenu getController() {
		return controller;
	}
	
	/*
	 * Mutators
	 */
	public void setController(ControllerMenu ctrl) {
		controller = ctrl;
	}
}
